﻿@using Radzen
@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore
@inject ContextMenuService ContextMenuService

@inherits DbContextPage

<p>Right click on a table cell to open the context menu.</p>
<RadzenDataGrid AllowFiltering="true" FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" AllowPaging="true" PageSize="4"
            AllowSorting="true" Data="@employees" TItem="Employee" ColumnWidth="200px"
            SelectionMode="DataGridSelectionMode.Single" @bind-Value=@selectedEmployees CellContextMenu="@OnCellContextMenu">
    <Columns>
        <RadzenDataGridColumn TItem="Employee" Property="Photo" Title="Employee" Sortable="false" Filterable="false">
            <Template Context="data">
                <RadzenImage Path="@data.Photo" style="width: 40px; height: 40px; border-radius: 8px; margin-right: 8px;" />
                @data.FirstName @data.LastName
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Employee" Property="Title" Title="Title" />
        <RadzenDataGridColumn TItem="Employee" Property="EmployeeID" Title="Employee ID" />
        <RadzenDataGridColumn TItem="Employee" Property="HireDate" Title="Hire Date" FormatString="{0:d}" />
        <RadzenDataGridColumn TItem="Employee" Property="City" Title="City" />
        <RadzenDataGridColumn TItem="Employee" Property="Country" Title="Country" />
    </Columns>
</RadzenDataGrid>

<EventConsole @ref=@console />

@code {
    IEnumerable<Employee> employees;
    IList<Employee> selectedEmployees;
    EventConsole console;

    void ClearSelection()
    {
        selectedEmployees = null;
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        employees = dbContext.Employees;
        selectedEmployees = employees.Take(1).ToList();
    }

    void OnCellContextMenu(DataGridCellMouseEventArgs<Employee> args)
    {
        selectedEmployees = new List<Employee>() { args.Data };

        ContextMenuService.Open(args,
            new List<ContextMenuItem> {
                new ContextMenuItem(){ Text = "Context menu item 1", Value = 1, Icon = "home" },
                new ContextMenuItem(){ Text = "Context menu item 2", Value = 2, Icon = "search" },
                new ContextMenuItem(){ Text = "Context menu item 3", Value = 3, Icon = "info" },
            }, 
            (e) => { 
             console.Log($"Menu item with Value={e.Value} clicked. Column: {args.Column.Property}, EmployeeID: {args.Data.EmployeeID}"); 
            }
         );
    }
}